home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Utilities / MemoryHelperƒ / Aevents.c next >
Encoding:
C/C++ Source or Header  |  1993-08-06  |  3.8 KB  |  157 lines  |  [TEXT/KAHL]

  1. #include <stdio.h>
  2. #include <GestaltEqu.h>
  3. #include <AppleEvents.h>
  4.  
  5. extern DoGet();
  6. extern OSErr OpenSelection(FSSpecPtr theDoc);
  7.  
  8. void    InitToolbox(void);
  9. Boolean AppleEventsInstalled (void);
  10. pascal OSErr  MyHandleODoc (AppleEvent *theAppleEvent, AppleEvent* reply, long
  11.                                                         handlerRefCon);
  12. pascal OSErr  MyHandlePDoc (AppleEvent *theAppleEvent, AppleEvent *reply, long
  13.                                                         handlerRefCon);
  14. pascal OSErr  MyHandleOApp (AppleEvent *theAppleEvent, AppleEvent *reply, long
  15.                                                         handlerRefCon);
  16. OSErr MyGotRequiredParams (AppleEvent *theAppleEvent);
  17.  
  18. Boolean gDone = FALSE;
  19.  
  20. void    InitToolbox()
  21. {
  22.     InitGraf(&thePort);        // Standard initialization calls
  23.     InitFonts();
  24.     InitWindows();
  25.     InitMenus();
  26.     TEInit();
  27.     InitDialogs(nil);
  28.     InitCursor();
  29. }
  30.  
  31. Boolean AppleEventsInstalled ()
  32. {
  33.     OSErr err;
  34.     long  result;
  35.  
  36.     // THINK C's MacTraps library provides glue for Gestalt, so
  37.     // it can be called safely under System 6. If an error is
  38.     // returned, then Gestalt for the AppleEvents Selector is
  39.     // not available (this also means that Apple Events are
  40.     // not available)
  41.     err = Gestalt (gestaltAppleEventsAttr, &result);
  42.     return (!err && ((result >> gestaltAppleEventsPresent) & 0x0001));
  43.                                             // return TRUE if there is no
  44.                                             // error and the proper bit of
  45.                                             // result is set
  46. }
  47.  
  48. pascal OSErr  MyHandleODoc (AppleEvent *theAppleEvent, AppleEvent* reply, long
  49.                                                         handlerRefCon)
  50. {
  51.     FSSpec    myFSS;
  52.     AEDescList    docList;
  53.     OSErr    err;
  54.     long    index,
  55.                     itemsInList;
  56.     Size    actualSize;
  57.     AEKeyword    keywd;
  58.     DescType    returnedType;
  59.  
  60.     // get the direct parameter--a descriptor list--and put it into a docList
  61.     err = AEGetParamDesc (theAppleEvent, keyDirectObject, typeAEList,
  62.             &docList);
  63.     if (err)
  64.             return err;
  65.  
  66.     // check for missing parameters
  67.     err = MyGotRequiredParams (theAppleEvent);
  68.     if (err)
  69.             return err;
  70.  
  71.     // count the number of descriptor records in the list
  72.     err = AECountItems (&docList, &itemsInList);
  73.  
  74.     // now get each descriptor record from the list, coerce the returned
  75.     // data to an FSSpec record, and open the associated file
  76.     for (index = 1; index <= itemsInList; index++) {
  77.  
  78.             err = AEGetNthPtr (&docList, index, typeFSS, &keywd,
  79.                             &returnedType, (Ptr) &myFSS, sizeof(myFSS),                 
  80. &actualSize);
  81.             if (err)
  82.                     return err;
  83.  
  84.                     OpenSelection(&myFSS);
  85.     }
  86.  
  87.     err = AEDisposeDesc (&docList);
  88.     ExitToShell();
  89. }
  90.  
  91. pascal OSErr  MyHandlePDoc (AppleEvent *theAppleEvent, AppleEvent *reply, long
  92.                                                         handlerRefCon)
  93. {
  94.     return noErr;
  95. }
  96.  
  97. pascal OSErr  MyHandleOApp (AppleEvent *theAppleEvent, AppleEvent *reply, long
  98.                                                         handlerRefCon)
  99. {
  100.     DoGet();
  101. }
  102.  
  103. OSErr MyGotRequiredParams (AppleEvent *theAppleEvent)
  104. {
  105.     DescType    returnedType;
  106.     Size    actualSize;
  107.     OSErr    err;
  108.  
  109.     err = AEGetAttributePtr (theAppleEvent, keyMissedKeywordAttr,
  110.                                     typeWildCard, &returnedType, nil, 0,
  111.                                     &actualSize);
  112.     if (err == errAEDescNotFound)    // you got all the required parameters
  113.             return noErr;
  114.     else if (!err)            // you missed a required parameter
  115.             return errAEEventNotHandled;
  116.     else                    // the call to AEGetAttributePtr failed
  117.             return err;
  118. }
  119.  
  120. main ()
  121. {
  122.     Boolean        aEvents;
  123.     short         doWhat;
  124.     short         fileCnt;
  125.     short        i;
  126.     AppFile        fileStuff;
  127.     EventRecord    theEvent;
  128.     OSErr        err;
  129.  
  130.     InitToolbox ();
  131.     aEvents = AppleEventsInstalled();
  132.     if (aEvents) 
  133.     {
  134.         err = AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments,
  135.                                                 MyHandleODoc,0, FALSE);
  136.         err = AEInstallEventHandler (kCoreEventClass, kAEOpenApplication,
  137.                                                 MyHandleOApp,0, FALSE);
  138.         err = AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments,
  139.                                                 MyHandlePDoc,0, FALSE);
  140.         while (!gDone) 
  141.         {
  142.             if ( WaitNextEvent ( everyEvent, &theEvent, 0, nil ) ) 
  143.             {
  144.                 switch (theEvent.what) 
  145.                 {
  146.                     case mouseDown:
  147.                         gDone = TRUE;
  148.                     case kHighLevelEvent:
  149.                         err = AEProcessAppleEvent (&theEvent);
  150.                         break;
  151.                 }
  152.             }
  153.         }
  154.     }
  155. }
  156.  
  157.